package com.wcfsy.wechat.wx.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.entity.PicImage;
import com.wcfsy.wechat.wx.entity.Product;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.ProductConfigDao;
import com.wcfsy.wechat.wx.sys.dao.ProductTypeDao;
import com.wcfsy.wechat.wx.util.DateStyle;
import com.wcfsy.wechat.wx.util.DateUtil;
import com.wcfsy.wechat.wx.util.FileUploadUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;

/**
 * 产品管理
 * 
 * @author yiqiang
 *
 */
@Controller
@RequestMapping("/cms/product")
public class ProductController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource
	private ProductConfigDao productConfigDao;
	@Resource
	private FileUploadUtil fileUploadUtil;
	
	@Resource
	private ProductTypeDao productTypeDao;

	@RequestMapping("/view/page")
	public String page() {
		return "product/product_list";
	}

	@RequestMapping("/view")
	@ResponseBody
	public IPage<Product> getOnAdConfigs(int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<Product>(
				this.productConfigDao.findByParam(paramMap),
				this.productConfigDao.count(paramMap), page, rows);
	}

	@RequestMapping("/modify/page")
	public ModelAndView preModify(int id) {
		Product info = productConfigDao.getById(id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("product/product_modify");
		mav.addObject("types", productTypeDao.queryProductTypes());
		mav.addObject("productImage", productConfigDao.getProductPic(id));
		mav.addObject("data", info);
		return mav;
	}

	@RequestMapping("/add/page")
	public ModelAndView preAdd() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("product/product_add");
		mav.addObject("data", productTypeDao.queryProductTypes());
		return mav;
	}

	@RequestMapping("/add")
	@ResponseBody
	public JsonMessage add(Product product,
			HttpServletRequest request) {
		MultipartHttpServletRequest multiReqest = (MultipartHttpServletRequest) request;
		MultipartFile imgUrlFile = multiReqest.getFile("minImageFile");
		List<MultipartFile> multipartFiles = multiReqest.getFiles("productImageFile");
		if(multipartFiles.size() <= 0){
			return JsonMessage.getErrorJson("产品介绍图片不能为空！");
		}
		try {
			if (imgUrlFile != null && !imgUrlFile.isEmpty()) {
				String productImage = fileUploadUtil.upload(imgUrlFile);
				product.setMinPic(productImage);
			} else {
				return JsonMessage.getErrorJson("产品小图片不能为空！");
			}
			product.setCreateDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			//会员节省的价格 原价- (原价*折扣)
			product.setSaveMuchMoney(product.getPrice() - (product.getPrice() * product.getDiscount()));
			//折后价格 原价*折扣
			product.setDiscountPrice(product.getPrice() * product.getDiscount());
			productConfigDao.add(product);
			if(!multipartFiles.isEmpty() && multipartFiles.size() > 0){
				PicImage picImage = null;
				for (MultipartFile multipartFile : multipartFiles) {
					picImage = new PicImage();
					String maxProductImage = fileUploadUtil.upload(multipartFile);
					picImage.setPicName(multipartFile.getOriginalFilename());
					picImage.setImageUrl(maxProductImage);
					picImage.setCreateDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
					picImage.setProductId(product.getId());
					picImage.setStats(1);
					productConfigDao.saveProductPic(picImage);
				}
			}
			return JsonMessage.getSuccessJson(null);
		} catch (Exception e) {
			logger.error("保存产品配置接口异常！", e);
			return JsonMessage.getErrorJson("保存产品配置接口异常！");
		}
	}

	/**
	 * 删除产品配置信息接口
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JsonMessage delete(int id) {
		try {
			productConfigDao.deleteById(id);
			productConfigDao.deleteByPicImage(id);
			return JsonMessage.getSuccessJson(null);
		} catch (Exception e) {
			logger.error("删除产品配置接口异常！", e);
			return JsonMessage.getErrorJson("删除产品配置接口异常！");
		}

	}

	/**
	 * 修改产品配置信息接口
	 * 
	 * @param productConfigInfo
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public JsonMessage modify(Product product,
			HttpServletRequest request) throws IOException {
		try {
			MultipartHttpServletRequest multiReqest = (MultipartHttpServletRequest) request;
			MultipartFile imgUrlFile = multiReqest.getFile("minImageFile");
			List<MultipartFile> multipartFiles = multiReqest.getFiles("productImageFile");
			if (imgUrlFile != null && !imgUrlFile.isEmpty()) {
				String productImage = fileUploadUtil.upload(imgUrlFile);
				product.setMinPic(productImage);
			}
			product.setUpdateDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			//会员节省的价格 原价- (原价*折扣)
			product.setSaveMuchMoney(product.getPrice() - (product.getPrice() * product.getDiscount()));
			//折后价格 原价*折扣
			product.setDiscountPrice(product.getPrice() * product.getDiscount());
			this.productConfigDao.update(product);
			if (multipartFiles != null && !multipartFiles.isEmpty() && multipartFiles.size() > 0) {
				PicImage picImage = null;
				productConfigDao.deleteImage(product.getId());
				for (MultipartFile multipartFile : multipartFiles) {
					picImage = new PicImage();
					String maxProductImage = fileUploadUtil.upload(multipartFile);
					picImage.setPicName(multipartFile.getOriginalFilename());
					picImage.setImageUrl(maxProductImage);
					picImage.setCreateDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
					picImage.setProductId(product.getId());
					picImage.setStats(1);
					productConfigDao.saveProductPic(picImage);
				}
			}
			return JsonMessage.getSuccessJson(null);
		} catch (Exception e) {
			logger.error("修改产品配置接口异常！", e);
			return JsonMessage.getErrorJson("修改产品配置接口异常！");
		}
	}

}
